#ifndef STRUCT_H
#define STRUCT_H

struct control_domain
{

	unsigned trans_drct_bit:1;		//transmission direction bit DIR 
	unsigned startup_flag_bit:1;		//startup flags bit PRM
	unsigned comm_mode:6;			// communication mode  
};

/*  information field    */
/* up message */
struct up_info_field
{
	unsigned relay_level:4;			//relay level
	unsigned confl_detec:1;			//collision detection
	unsigned com_module_flag:1;		//communication module flag
	unsigned sub_node_flag:1;		//sub node flag
	unsigned route_flag:1;			//route flag
	unsigned correct_code_flag:4;		//correct code flag
	unsigned channel_flag:4;		//channel flag
};

/* reply message */
struct _reply
{
	unsigned pre_reply_num:8;		//predict reply number
	unsigned rate_unit_flag:1;		//rate unit flag
	unsigned comm_rate:15;			//communicationg rate
	unsigned reserve:8;			//reserve
};

/* down message  */
struct down_info_field
{
	unsigned relay_level:4;			//relay level
	unsigned zero_bit_1:1 = 0;		//zero bit
	unsigned com_module_flag:1;		//communication module flag
	unsigned zero_bit_2:1 = 0;		//zero bit
	unsigned route_flag:1;			//router flag
	unsigned zero_bit_3:4 = 0;		//zero bit
	unsigned channel_flag:4;		//channel flag
	unsigned ammeter_gall_charac:4;		//ammeter gallery charactor
	unsigned phasel_actual_flag:4;		//actual measuremen phase line flag
	unsigned lreply_messq:4;		//last reply message quality
	unsigned forder_messq:4;		//last order message quality
	unsigned reserve:16;			//reserve
};

union Info_field
{
	struct up_info_field info_field_up;	//info_field up message	
	struct down_info_field info_field_down;	//info_field down message
};

/*     address field       */
union addr_field
{
	unsigned source_addr:48;		//source address
	unsigned relay_addr:48 * relay_level;	//relay address
	unsigned Destnt_addr:48;		//dest address
};

/*      data unit struct         */
/*  AFN = 00H*/
struct data_unit_aff
{
	unsigned channel_state:16;		//channel state
	unsigned wait_time:16;			//waitting tiom
};

union data_unit_aff
{
	struct data_unit_aff data_aff;		//affirm message
	unsigned error_state:8;			//error start
};

/*   AFN = 02H  */
struct data_unit_tsend
{
	unsigned type:8;			//code type
	unsigned mess_length:8;			//message length
	unsigned char mess_content[256];	//message content
};

/* AFN = 03H  */
/* down message  */
struct data_unit_sdown
{
	unsigned snodep:8;			//start node pointer
	unsigned rnoden:8;			//read node number
};

union data_unit_search_down
{
	struct data_unit_sdown data_unit_sdown;	//DT = F3;carrier fnode interception info
	unsigned con_time:8;			//DT = F6;carrier mnode disturbance state
};

union data_unit_search
{
	union data_unit_search_up data_sup; 	//search up message
	union data_unit_search_down data_sdown;	//search down message
};

/* up message  */
struct data_unit_supf1  			//factory code and version info
{
	unsigned factory_id:16; 		//factory id
	unsigned chip_id:16;			//chip id
	unsigned version_day:8;			//version day
};

struct data_unit_supf2 				//noise value
{
	unsigned reserve:4; 			//reserve
	unsigned strnoise:4;			//noise strength
};

struct data_unit_supf3				//carrier fnode interception info
{
	unsigned lnode_sum:8;			//interception node sum
	unsigned lcocnode_num:8;		//interception trans node number
	unsigned faddress:48;			//fnode address
	unsigned lsignalq:4;			//interception signal quality
	unsigned relay_level:4;			//relay level
	unsigned reserve:3;			//reserve
	unsigned ltimes:5;			//interception times
};

unsigned mainnode_add:48; 			//F4;carrier main node address

Struct data_unit_supf5				//F5;carrier mnode state word and rate
{
	unsigned state_word:16;			//state word
	unsigned rate_flag:1;			//rate flag
	unsigned carrier_commrate:15;		//carrier communication rate
};

unsigned disturb_state:8;			//F6 carrier main node disturb state

union data_unit_search_up
{
	struct data_unit_supf1 dunit_supf1;	//F1 factory code and version info
	struct data_unit_supf2 dunit_supf2;	//F2 noise value
	struct data_unit_supf3 dunit_supf3;	//F3 carrier fnode interception info 
	unsigned mainnode_add:48; 		//F4 carrier main node address
	struct data_unit_supf5 dunit_supf5;	//F5 carrier mnode state word and rate
	unsigned disturb_state:8;		//F6 carrier main node disturb state
};

/* message*/
union data_unit_search
{
	union data_unit_search_up data_sup;	// search up message
	union data_unit_search_down data_sdown;	// search down message
};

/* AFN = 04H */
/* down message*/
unsigned con_time:8;				//send test

/*  up message  */
union data_unit_ldetec_up
{
	unsigned carrier_mnode_add:48;		//F1:set carrier mnode address
	struct data_unit_aff carrier_ldetec;	//other
};

/*  message */
union data_unit_ldetec
{
	union data_unit_ldetec_up data_ldetecup;
	unsigned con_time:8;
};

/* AFN = 05H*/
/*  down message  */
/*  start broadcast*/
struct data_unit_corder_downf2
{
	unsigned control_word:8;		//control word
	unsigned mess_length:8;			//message length
	unsigned char mess_content[256];	//message content
};

union data_unit_corder_down
{
	unsigned carrier_mnode_add:48;		//F1:set carrier main node address
	struct data_unit_corder_downf2 data_ucoderd;//start broadcast
};

union data_unit_corder
{
	union data_unit_corder_down data_corderd;// down message
	struct data_unit_aff corder;		 //up message
};

/* AFN = 06H */
/*  up message  */
struct data_unit_inirpt_upf1
{
	unsigned rcnode_num:8;			//report cfnode number
	unsigned cnode_addr:48;			//carrier fnode address
	unsigned cnode_type:8;			//carrier fnode type
	unsigned cnode_id:16;			//carrier fnode id
};

struct data_unit_inirpt_upf2
{
	unsigned cnode_id:16;			//carrier fnode id
	unsigned type:8;			//type
	unsigned mess_length:8;			//message length
	unsigned char mess_content[256];	//message content
};

union data_unit_inirpt_up
{
	struct data_unit_inirpt_upf1 data_inirptupf1;//F1:report cfnode info
	struct data_unit_inirpt_upf2 data_inirptupf2;//F2:report over data
};

union data_unit_inireport
{
	union data_unit_inirpt_up data_inirpt_up;//up message
	struct data_unit_aff inireport;		 // down message
};

/*  AFN = 10H */
/*  up message */
struct data_unit_rsearch_upf1		//F1:carrier fnode number
{
	unsigned cnode_sum:16;			//fnode sum
	unsigned rmain_fnode_num:16;		//route sustain fnode max number
};

struct fnode_info
{
	unsigned lsigq:4;			//interception signal quality
	unsigned relay_level:4;			//relay level
	unsigned reserve:5;			//reserve
	unsigned phase:3;			//phase
};

struct data_unit_rsearch_upf2			//F2:carrier fnode info
{
	unsigned cnode_sum:16;			//fnode number
	unsigned thre_fnode_num:8;		//this time reply fnode number
	unsigned fnode_add:48;			//fnode address
	struct fnode_info fnode_info;		//fnode information
};

struct data_unit_rsearch_upf3			//F3:cfnode's last level mid router info
{
	unsigned offerr_fnode_sum:8;		//offer route fnode sum
	unsigned offerr_fnode_add:48;		//fnode address
	struct fnode_info offerr_fnode_info;	//fnode information
};

struct run_state_word
{
	unsigned correct_code:4;		//correct code
	unsigned reserve:1;			//reserve
	unsigned report_flag:1;			//report event flag
	unsigned work_flag:1;			//work flag
	unsigned rfinish_flag:1;		//route finish flag
};

struct work_switch
{
	unsigned reserve:6;			//reserve
	unsigned regist_alow_state:1;		//regist alow state
	unsigned work_state:1;			//work state
};

struct data_unit_rsearch_upf4			//F4:route running state
{
	struct run_state_word run_sword;	//running state words
	unsigned fnode_sum:16;			//fnode sum
	unsigned wfnode_sum:16;			//copyed fnode sum
	unsigned rton_sum:16;			//relay copy to fnode number
	struct work_switch work_switch;		//work switch
	unsigned carrier_comrate:16;		//carrier communication rate
	unsigned first_relay_level:8;		//first relay level
	unsigned second_relay_level:8;		//second relay level
	unsigned third_relay_level:8;		//third relay level
	unsigned first_work_step:8;		//first work step
	unsigned second_work_step:8;		//second work step
	unsigned third_work_step:8;		//third work step
};

struct data_unit_rsearch_upf5			//F5: not success copyed cfnode info 
{
	unsigned cnode_sum:16;			//fnode number
	unsigned thre_fnode_num:8;		//this time reply fnode number
	unsigned fnode_add:48;			//fnode address
	struct fnode_info fnode_info;		//fnode information
};

struct data_unit_rsearch_upf6
{
	unsigned cndoe_sum:16;			//fnode number
	unsigned thre_fnode_num:8;		//this time reply fndoe number
	unsigned fnode_add:48;			//fnode address
	struct fnode_info fnode_info;		//fnode information
};

union data_unit_rsearch_up
{
	struct data_unit_rsearch_upf1 rsupf1;	//F1:carrier fnode number
	struct data_unit_rsearch_upf2 rsupf2;	//F2:carroer fnode info
	struct data_unit_rsearch_upf3 rsupf3;	//F3:fnode last midroute info
	struct data_unit_rsearch_upf4 rsupf4;	//F4:route ronning state
	struct data_unit_rsearch_upf5 rsupf5;	//F5:unsuccess cfnode info
	struct data_unit_rsearch_upf6 rsupf6;	//regist cfnode info
};

/*  down message  */
struct data_unit_rsearch_downf2			//F2:carrier fndoe info
{
	unsigned fnstart_num:16;		//fnode start id
	unsigned fnode_num:8;			//fnode num
};

struct data_unit_rsearch_downf5			//F5:unsucceed copy cfnode info
{
	unsigned fnstart_num:16;		//fnode start id
	unsigned fnode_num:8;			//fnode num
};

struct data_unit_rsearch_downf6			//F6:regist carrier fnode info
{
	unsigned fnstart_num:16;		//fnode start id
	unsigned fnode_num:8;			//fnode number
};

union data_unit_rsearch_down
{
	struct data_unit_rsearch_downf2 data_rsearchdf2;//F2:carrier fnode info
	unsigned fnadd:48;			//F3:cfnode last level midroute info
	struct data_unit_rsearch_downf5 data_rsearchdf5;//F5:unsuccess copy cfnode info
	struct data_unit_rsearch_downf6 data_rsearchdf6;//F6:registed cfnode info
};

union data_unit_rsearch
{
	union data_unit_rsearch_up data_rsearchu;//up messafe
	union data_unit_rsearch_down data_rsearchd;//down message
};

/*  AFN = 11H  */
/*  down message  */
struct data_unit_rset_downf1			//F1:insert carrier fnode
{
	unsigned cnode_sum:8;			//fnode number
	unsigned fnode_add:48;			//fnode address
	unsigned fnode_id:16;			//fnode id
	unsigned fnode_type:8;			//fnode type
};

struct data_unit_rset_downf2			//F2:delete carrier fndoe
{
	unsigned cnode_sum:8;			//fnode number
	unsigned fnode_add:48;			//fnode address
};

struct work_mode
{
	unsigned correct_code:4;		//correct code
	unsigned reserve:2;			//reserve
	unsigned regist_alow_state:1		//regist alow state
	unsigned work_state:1;			//work state
};

struct data_unit_rset_downf4			//F4:set route work mode
{
	struct work_mode work_mode;		//work mode
	unsigned rate_flag:1;			//rate units flags
	unsigned carrier_comrate:15;		//carrier communication rate
};

struct start_time
{
	unsigned sdecimal:4;			//second decimal
	unsigned sunits:4;			//second units
	unsigned midecimal:4;			//minite decimal
	unsigned miunits:4;			//minite units
	unsigned hdecimal:4;			//hour decimal
	unsigned hunits:4;			//hour units
	unsigned ddecimal:4;			//day decimal
	unsigned dunits:4;			//day units
	unsigned mdecimal:4;			//month decimal
	unsigned munits:4;			//month units
	unsigned ydecimal:4;			//year decimal
	unsigned yunits:4;			//year units
};

struct data_unit_rset_downf5			//F5:cfnode regist
{
	struct start_time start_time;		//start_time
	unsigned con_time:16;			//continuw time
	unsigned fnode_rsend_times:8;		//fnode resent time
	unsigned wait_tchip_num:8;		//waitting time chip number
};

union data_unit_rset_down			//route set 
{
	struct data_unit_rset_downf1 data_rsdf1;//F1:insert carrier fnode
	struct data_unit_rset_downf2 data_rsdf2;//F2:delete carrier fnode
	struct data_unit_rset_downf4 data_rsdf4;//F4:set route work mode
	struct data_unit_rset_downf5 data_rsdf5;//F5:active cfnode regist
};

union data_unit_rset
{
	union data_unit_rset_down data_rsetd;	//down message
	struct data_unit_aff rsetu;		//up message			
};

/* AFN = 13H*/
/*  down message  */
struct data_unit_rtransmit_down
{
	unsigned type:8;			//type
	unsigned fnode_subn_num:8;		//fnode' subnode number
	unsigned fnode_subn_add:48;		//fnode' subnode address
	unsigned mess_length:8;			//message length
	unsigned char mess_conrent;		//message content
};

/*  up message  */
struct data_unit_rtransmit_up
{
	unsigned type:8;			//type
	unsigned mess_length:8;			//message length
	unsigned char mess_content[256];	//message content
};

union data_unit_rtransmit
{
	struct data_unit_rtransmit_down data_rtdown;//down message
	struct data_unit_rtransmit_up data_rtup;    // up message
};

/*  AFN = 14H  */
/*  up message  */
struct data_unit_rinfo_rwup
{
	unsigned carrier_comm_phase:8;		//carrier communication phase
	unsigned carroer_fnode_addr:48;		//carrier fnode address
	unsigned carrier_fnode_id:16;		//carrier fnode id
};

/*  down message  */
struct data_unit_rinfo_rwdown
{
	unsigned rw_flag:8;			//rw flag
	unsigned data_length:8;			//data length
	unsigned char data_content[256];	//data content
	unsigned cfnode_subn_num:8;		//cfnode's subnode number
	unsigned cfnode_subn_add:48;		//cfnode's subnode address
};

union data_unit_rinfo_rw
{
	struct data_unit_rinfo_rwdown data_rrwdown;// down message
	struct data_unit_rinfo_rwup data_rrwup;	   // up message
};

union data_unit
{
	union data_unit_aff data_unit_aff00;
	union data_unit_aff data_unit_aff01;
	struct data_unit_tsend data_unit_tsent02;
	union data_unit_search data_unit_search03;
	union data_unit_ldetec data_unit_ldetec04;	
	union data_unit_corder data_unit_corder05;
	union data_unit_inireport data_unit_inireport06;
	union data_unit_rsearch data_unit_rsearch10;
	union data_unit_rset data_unit_rset11;
	union data_unit_aff data_unit_rcontrol12;
	union data_unit_rtransmit data_unit_rtransmin13;
	union data_unit_rinfo_rw data_unit_rinforw14;
	union dara_unit_aff data_unit_rconclassF0;
};

struct _smart_grids
{

	unsigned start:8 = 68H;			//86H	start bit
	unsigned mess_len:16;			//L	message length
	struct control_domain app_condom;	//C	control domain
	union Info_field  info_field; 		//R	information field
	union addr_field address;		//A	address field
	unsigned app_func_code:8;		//AFN	application function code
	unsigned data_unit_flag:16;		//DT	data unit flags
	union data_unit data_unit;		//data 	unit struct
	unsigned checksum:8;			//CS	checksum
	unsigned end:8;				//16H end bit

};


#endif
